package com.fau.hm.service.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Hibernate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fau.hm.entity.MedicalCallCheck;
import com.fau.hm.entity.MedicalNuclearCheck;
import com.fau.hm.service.ICheckManagerService;
import com.fau.hm.util.DateUtil;
import com.fau.hm.util.Page;
import com.fau.hm.util.ParamUtil;
import com.fau.hm.util.SystemConfig;

/**
 * @desc 绩效考核模块
 * @author dangjh
 * @datetime 2014年10月22日,下午9:28:03
 */
@Service
@Transactional(readOnly = true)
public class CheckManagerService extends BaseService implements ICheckManagerService {

	@Override
	public Page findNuclearUsers(Map<String, Object> map, MedicalNuclearCheck bean) {
		StringBuilder statusSql = new StringBuilder("from MedicalNuclearCheck a where 1=1 and a.eid = ? ");
		StringBuilder countSql = new StringBuilder("select count(distinct a.user.id) from MedicalNuclearCheck a where 1=1 and a.eid = ? ");
		Date startTime = new Date();
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(bean.getEid());
		if(StringUtils.isNotBlank(bean.getName())){
			String info = bean.getName().replaceAll("_", "chr(95)").replaceAll("%", "chr(37)");
			statusSql.append(" and a.user.name like ? ");
			countSql.append(" and a.user.name like ? ");
			objectList.add("%"+info+"%");
		}
		
		if(null != bean.getStartTime()){
			startTime = bean.getStartTime();
		}
		statusSql.append(" and (a.createTime between ? and ?)");
		countSql.append(" and (a.createTime between ? and ?)");
		objectList.add(DateUtil.getDate(startTime, 0));
		objectList.add(DateUtil.getDate(startTime, 1));
		
		statusSql.append(" group by a.user.id order by a.createTime desc");
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalNuclearCheck> list = new ArrayList<MedicalNuclearCheck>();
		for(Object obj : page.getResult()) {
			MedicalNuclearCheck nuclear = (MedicalNuclearCheck) obj;
			int allOrderNum = this.iBaseDao.getIntCount("select count(*) from MedicalNuclearCheck m where m.user.id = ? "
					+ " and m.createTime between ? and ? ", nuclear.getUser().getId(), DateUtil.getDate(startTime, 0), DateUtil.getDate(startTime, 1));
			int orderNum = this.iBaseDao.getIntCount("select count(*) from MedicalNuclearCheck m where m.user.id = ? and m.status"
					+ " = ? and m.createTime between ? and ? ", nuclear.getUser().getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE, 
					DateUtil.getDate(startTime, 0), DateUtil.getDate(startTime, 1));
			nuclear.setAllOrderNum(allOrderNum);
			nuclear.setOrderNum(orderNum);
			
			// 普通用户
			if(!SystemConfig.SUPER_ADMIN.equals(nuclear.getUser().getIsSuper())) {
				Hibernate.initialize(nuclear.getUser().getGroup());
			}
			
			double per = (Double.valueOf(orderNum) / Double.valueOf(allOrderNum)) * 100;
			
			DecimalFormat df = new DecimalFormat("0.00");
			nuclear.setPercentage(df.format(per)+"%");
			list.add(nuclear);
		}
		page.setResult(list);
		return page;
	}
	
	@Override
	public Page findCallUsers(Map<String, Object> map, MedicalCallCheck bean) {
		StringBuilder statusSql = new StringBuilder("from MedicalCallCheck a where 1=1 and a.eid = ? ");
		StringBuilder countSql = new StringBuilder("select count(distinct a.user.id) from MedicalCallCheck a where 1=1 and a.eid = ? ");
		Date startTime = new Date();
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(bean.getEid());
		
		if(StringUtils.isNotBlank(bean.getName())){
			String info = bean.getName().replaceAll("_", "chr(95)").replaceAll("%", "chr(37)");
			statusSql.append(" and a.user.name like ? ");
			countSql.append(" and a.user.name like ? ");
			objectList.add("%"+info+"%");
		}
		
		if(null != bean.getStartTime()) {
			startTime = bean.getStartTime();
		}
		statusSql.append(" and (a.createTime between ? and ?)");
		countSql.append(" and (a.createTime between ? and ?)");
		objectList.add(DateUtil.getDate(startTime, 0));
		objectList.add(DateUtil.getDate(startTime, 1));
		
		statusSql.append(" group by a.user.id order by a.createTime desc");
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalCallCheck> list = new ArrayList<MedicalCallCheck>();
		for(Object obj : page.getResult()) {
			MedicalCallCheck callCheck = (MedicalCallCheck) obj;
			int allTaskNum = this.iBaseDao.getIntCount("select count(*) from MedicalCallCheck m where m.user.id = ? "
					+ " and m.createTime between ? and ? ", callCheck.getUser().getId(), DateUtil.getDate(startTime, 0), DateUtil.getDate(startTime, 1));
			int deliveryNum = this.iBaseDao.getIntCount("select count(*) from MedicalCallCheck m where m.user.id = ? and m.status"
					+ " = ? and m.createTime between ? and ? ", callCheck.getUser().getId(), SystemConfig.CALL_TASK_END,
					 DateUtil.getDate(startTime, 0), DateUtil.getDate(startTime, 0));
			callCheck.setAllTaskNum(allTaskNum);
			callCheck.setDeliveryNum(deliveryNum);
			
			// 普通用户
			if(!SystemConfig.SUPER_ADMIN.equals(callCheck.getUser().getIsSuper())) {
				Hibernate.initialize(callCheck.getUser().getGroup());
			}
						
			double per = (Double.valueOf(deliveryNum) / Double.valueOf(allTaskNum)) * 100;
			callCheck.setPercentage(per+"%");
			list.add(callCheck);
		}
		page.setResult(list);
		return page;
	}

	@Override
	public Page findNuclearGroups(Map<String, Object> map, MedicalNuclearCheck bean) {
		StringBuilder statusSql = new StringBuilder("from MedicalNuclearCheck a where 1=1 and a.eid = ? ");
		StringBuilder countSql = new StringBuilder("select count(distinct a.user.group.id) from MedicalNuclearCheck a where 1=1 and a.eid = ? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(bean.getEid());
		Date startTime = new Date();
		if(StringUtils.isNotBlank(bean.getName())){
			String info = bean.getName().replaceAll("_", "chr(95)").replaceAll("%", "chr(37)");
			statusSql.append(" and a.user.group.name like ? ");
			countSql.append(" and a.user.group.name like ? ");
			objectList.add("%"+info+"%");
		}
		
		if(null != bean.getStartTime()) {
			startTime = bean.getStartTime();
		}
		statusSql.append(" and (a.createTime between ? and ?)");
		countSql.append(" and (a.createTime between ? and ?)");
		objectList.add(DateUtil.getDate(startTime, 0));
		objectList.add(DateUtil.getDate(startTime, 1));
		
		statusSql.append(" group by a.user.group.id order by a.createTime desc");
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalNuclearCheck> list = new ArrayList<MedicalNuclearCheck>();
		for(Object obj : page.getResult()) {
			MedicalNuclearCheck nuclear = (MedicalNuclearCheck) obj;
			int allOrderNum = 0;
			int orderNum = 0;
			// 普通用户
//			if(!SystemConfig.SUPER_ADMIN.equals(nuclear.getUser().getIsSuper())) {
			Hibernate.initialize(nuclear.getUser().getGroup());
			allOrderNum = this.iBaseDao.getIntCount("select count(*) from MedicalNuclearCheck m where m.user.group.id = ? "
					+ " and m.createTime between ? and ? ", nuclear.getUser().getGroup().getId(), DateUtil.getDate(startTime, 0), DateUtil.getDate(startTime, 1));
			orderNum = this.iBaseDao.getIntCount("select count(*) from MedicalNuclearCheck m where m.user.group.id = ? and m.status"
					+ " = ? and m.createTime between ? and ? ", nuclear.getUser().getGroup().getId(), SystemConfig.ORDERS_AUDIT_PASS_TYPE, 
					DateUtil.getDate(startTime, 0), DateUtil.getDate(startTime, 1));
//			}else{	// 超级管理员
//				allOrderNum = this.iBaseDao.getIntCount("select count(*) from MedicalNuclearCheck m where m.createTime between ? and ? ",
//						DateUtil.getDate(startTime, 0),
//						DateUtil.getDate(startTime, 1));
//				orderNum = this.iBaseDao.getIntCount("select count(*) from MedicalNuclearCheck m where m.status = ? and m.createTime between ? and ? ",
//						SystemConfig.ORDERS_AUDIT_PASS_TYPE, 
//						DateUtil.getDate(startTime, 0),
//						DateUtil.getDate(startTime, 1));
//			}
			nuclear.setAllOrderNum(allOrderNum);
			nuclear.setOrderNum(orderNum);
			double per = (Double.valueOf(orderNum) / Double.valueOf(allOrderNum)) * 100;
			nuclear.setPercentage(per+"%");
			list.add(nuclear);
		}
		page.setResult(list);
		return page;
	}
	
	@Override
	public Page findCallGroups(Map<String, Object> map, MedicalCallCheck bean) {
		StringBuilder statusSql = new StringBuilder("from MedicalCallCheck a where 1=1 and a.eid = ? ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallCheck a where 1=1 and a.eid = ? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(bean.getEid());
		Date startTime = new Date();
		if(StringUtils.isNotBlank(bean.getName())){
			String info = bean.getName().replaceAll("_", "chr(95)").replaceAll("%", "chr(37)");
			statusSql.append(" and a.user.group.name like ? ");
			countSql.append(" and a.user.group.name like ? ");
			objectList.add("%"+info+"%");
		}
		
		if(null != bean.getStartTime()) {
			startTime = bean.getStartTime();
		}
		statusSql.append(" and (a.createTime between ? and ?)");
		countSql.append(" and (a.createTime between ? and ?)");
		objectList.add(DateUtil.getDate(startTime, 0));
		objectList.add(DateUtil.getDate(startTime, 1));
		
		statusSql.append(" group by a.user.group.id order by a.createTime desc");
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalCallCheck> list = new ArrayList<MedicalCallCheck>();
		
		for(Object obj : page.getResult()) {
			MedicalCallCheck call = (MedicalCallCheck) obj;
			int allTaskNum = 0;
			int deliveryNum = 0;
			// 普通用户
			Hibernate.initialize(call.getUser().getGroup());
			allTaskNum = this.iBaseDao.getIntCount("select count(*) from MedicalCallCheck m where m.user.group.id = ? "
					+ " and m.createTime between ? and ? ", call.getUser().getGroup().getId(), DateUtil.getDate(startTime, 0), DateUtil.getDate(startTime, 1));
			deliveryNum = this.iBaseDao.getIntCount("select count(*) from MedicalCallCheck m where m.status = ? and m.createTime between ? and ? ", SystemConfig.CALL_TASK_END, 
					DateUtil.getDate(startTime, 0), DateUtil.getDate(startTime, 1));
			call.setAllTaskNum(allTaskNum);
			call.setDeliveryNum(deliveryNum);
			double per = (Double.valueOf(deliveryNum) / Double.valueOf(allTaskNum)) * 100;
			call.setPercentage(per+"%");
			list.add(call);
		}
		page.setResult(list);
		return page;
	}
	
}
